1 package attr;
2
3 import java.lang.*;
4 import java.util.*;
5 import java.text.*;
6 import javax.swing.*;
7 import javax.swing.table.*;
8 import java.sql.*;
9 import attr.*;
10 import activity.*;

11
12 public
class Customer extends User {
13     
private String customerName;
14     
private String phoneNumber;
15     
private String address;
16     
public static String[] columnNames = {"PurchaseID", "ProductID", "ProductName", "Amount", "Cost", "Date"};
17     
public static String[] columnName = {"CustomerID", "CustomerName", "PhoneNumber", "Address"};
18     
public Customer(String userId) {
19         super(userId);
20         
21         
this.setStatus(1);
22     }
23     
24     
public void setCustomerName(String name) {
25         
if (!name.isEmpty())
26             
this.customerName = name;
27         
else
28             
throw new IllegalArgumentException("Fill in the name");
29     }
30     
public void setPhoneNumber(int num) {
31         
this.phoneNumber = "+880"+num;
32     }
33     
public void setAddress(String address) {
34         
if (!address.isEmpty())
35             
this.address = address;
36         
else
37             
throw new IllegalArgumentException("Fill in the address");
38     }
39     
public String getCustomerName() {
40         
return customerName;
41     }
42     
public String getPhoneNumber() {
43         
return phoneNumber;
44     }
45     
public String getAddress() {
46         
return address;
47     }
48
49     
public void createCustomer(JFrame sa) {
50         String query1 =
"INSERT INTO `login` VALUES ('"+userId+"','"+password+"',"+status+");";
51         String query2 =
"INSERT INTO `customer` VALUES ('"+userId+"','"+customerName+"','"+phoneNumber+"','"+address+"');";
52         Connection con =
null;
53         Statement st =
null;
54         System.
out.println(query1);
55         System.
out.println(query2);
56         
try {
57             Class.forName(
"com.mysql.jdbc.Driver");
58             System.
out.println("driver loaded");
59             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
60             System.
out.println("connection done");//connection with database established
61             st = con.createStatement();
//create statement
62             System.
out.println("statement created");
63             st.execute(query1);
//insert
64             st.execute(query2);
65             System.
out.println("data inserted");
66             JOptionPane.showMessageDialog(sa,
"Account Created!");
67             sa.setVisible(
false);
68             
new LoginActivity().setVisible(true);
69         }
70         
catch(Exception ex) {
71             JOptionPane.showMessageDialog(sa,
"Failed to create account!");
72             System.
out.println("Exception : " +ex.getMessage());
73         }
74         
finally {
75             
try {
76                 
if(st!=null)
77                     st.close();
78
79                 
if(con!=null)
80                     con.close();
81             }
82             
catch(Exception ex) {}
83         }
84     }
85     
86     
public void fetch() {
87         String query =
"SELECT `userId`, `customerName`, `phoneNumber`, `address` FROM `customer` WHERE userId='"+this.userId+"';";
88         Connection con =
null;
89         Statement st =
null;
90         ResultSet rs =
null;
91         System.
out.println(query);
92         
try {
93             Class.forName(
"com.mysql.jdbc.Driver");
94             System.
out.println("driver loaded");
95             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
96             System.
out.println("connection done");//connection with database established
97             st = con.createStatement();
//create statement
98             System.
out.println("statement created");
99             rs = st.executeQuery(query);
//getting result
100             System.
out.println("results received");
101             
102             
while(rs.next()) {
103                 
this.customerName = rs.getString("customerName");
104                 
this.phoneNumber = rs.getString("phoneNumber");
105                 
this.address = rs.getString("address");
106             }
107         }
108         
catch(Exception ex) {
109             System.
out.println("Exception : " +ex.getMessage());
110         }
111         
finally {
112             
try {
113                 
if(rs!=null)
114                     rs.close();
115
116                 
if(st!=null)
117                     st.close();
118
119                 
if(con!=null)
120                     con.close();
121             }
122             
catch(Exception ex) {}
123         }
124     }
125     
126     
public void updateCustomer(String name, int phone, String address) {
127         String query =
"UPDATE `customer` SET `customerName`='"+name+"', `phoneNumber`='+880"+phone+"', `address`='"+address+"' WHERE `userId`='"+this.userId+"';";
128         Connection con =
null;
129         Statement st =
null;
130         System.
out.println(query);
131         
try {
132             Class.forName(
"com.mysql.jdbc.Driver");
133             System.
out.println("driver loaded");
134             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
135             System.
out.println("connection done");//connection with database established
136             st = con.createStatement();
//create statement
137             System.
out.println("statement created");
138             st.executeUpdate(query);
//insert
139             System.
out.println("data inserted");
140             JOptionPane.showMessageDialog(
null,"Information Updated!");
141             
this.customerName = name;
142             
this.phoneNumber = "+880"+phone;
143             
this.address = address;
144         }
145         
catch(Exception ex) {
146             JOptionPane.showMessageDialog(
null,"Failed to update account!");
147             System.
out.println("Exception : " +ex.getMessage());
148         }
149         
finally {
150             
try {
151                 
if(st!=null)
152                     st.close();
153
154                 
if(con!=null)
155                     con.close();
156             }
157             
catch(Exception ex) {}
158         }
159     }
160     
public void deleteCustomer() {
161         String query1 =
"DELETE FROM `login` WHERE `userId`='"+this.userId+"';";
162         String query2 =
"DELETE FROM `customer` WHERE `userId`='"+this.userId+"';";
163         Connection con =
null;
164         Statement st =
null;
165         System.
out.println(query1);
166         System.
out.println(query2);
167         
try {
168             Class.forName(
"com.mysql.jdbc.Driver");
169             System.
out.println("driver loaded");
170             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
171             System.
out.println("connection done");//connection with database established
172             st = con.createStatement();
//create statement
173             System.
out.println("statement created");
174             st.execute(query1);
175             st.execute(query2);
//delete
176             System.
out.println("data deleted");
177             JOptionPane.showMessageDialog(
null,"Account Deleted!");
178             
this.userId = "";
179             
this.customerName = "";
180             
this.phoneNumber = "";
181             
this.address = "";
182         }
183         
catch(Exception ex) {
184             JOptionPane.showMessageDialog(
null,"Failed to delete account!");
185             System.
out.println("Exception : " +ex.getMessage());
186         }
187         
finally {
188             
try {
189                 
if(st!=null)
190                     st.close();
191
192                 
if(con!=null)
193                     con.close();
194             }
195             
catch(Exception ex) {}
196         }
197     }
198     
199     
public DefaultTableModel myProduct() {
200         DefaultTableModel model =
new DefaultTableModel();
201         model.setColumnIdentifiers(columnNames);
202         String query =
"SELECT purchaseInfo.purchaseId, purchaseInfo.productId, product.productName, purchaseInfo.cost, purchaseInfo.amount, purchaseInfo.date FROM purchaseInfo, product WHERE (`purchaseInfo`.`userId`='"+this.userId+"' AND `purchaseInfo`.`productId`=`product`.`productId`) ORDER BY `date` DESC;";
203         Connection con =
null;
204         Statement st =
null;
205         ResultSet rs =
null;
206         System.
out.println(query);
207         
try {
208             Class.forName(
"com.mysql.jdbc.Driver");
209             System.
out.println("driver loaded");
210             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
211             System.
out.println("connection done");//connection with database established
212             st = con.createStatement();
//create statement
213             System.
out.println("statement created");
214             rs = st.executeQuery(query);
//getting result
215             System.
out.println("results received");
216             
217             
while(rs.next()) {
218                 String col1 = rs.getString(
"purchaseId");
219                 String col2 = rs.getString(
"productId");
220                 String col3 = rs.getString(
"productName");
221                 
int col4 = rs.getInt("amount");
222                 
double col5 = rs.getDouble("cost");
223                 String col6 = rs.getString(
"date");
224                 model.addRow(
new Object[]{col1, col2, col3, col4, col5, col6});
225             }
226         }
227         
catch(Exception ex) {
228             System.
out.println("Exception : " +ex.getMessage());
229         }
230         
finally {
231             
try {
232                 
if(rs!=null)
233                     rs.close();
234
235                 
if(st!=null)
236                     st.close();
237
238                 
if(con!=null)
239                     con.close();
240             }
241             
catch(Exception ex) {}
242         }
243         
return model;
244     }
245     
246     
public static DefaultTableModel searchCustomer(String keyword, String byWhat) {
247         DefaultTableModel model =
new DefaultTableModel();
248         model.setColumnIdentifiers(columnName);
249         String query =
"SELECT * FROM `customer` WHERE `userId`='"+keyword+"';";
250         
if (byWhat.equals("By Name"))
251             query =
"SELECT * FROM `customer` WHERE `customerName` LIKE '%"+keyword+"%';";
252         
else {}
253         Connection con =
null;
254         Statement st =
null;
255         ResultSet rs =
null;
256         System.
out.println(query);
257         
try {
258             Class.forName(
"com.mysql.jdbc.Driver");
259             System.
out.println("driver loaded");
260             con = DriverManager.getConnection(Database.HOST_URI, Database.USER, Database.PASSWORD);
261             System.
out.println("connection done");//connection with database established
262             st = con.createStatement();
//create statement
263             System.
out.println("statement created");
264             rs = st.executeQuery(query);
//getting result
265             System.
out.println("results received");
266             
267             
while(rs.next()) {
268                 model.addRow(
new Object[]{rs.getString("userId"), rs.getString("customerName"), rs.getString("phoneNumber"), rs.getString("address")});
269             }
270         }
271         
catch(Exception ex) {
272             System.
out.println("Exception : " +ex.getMessage());
273         }
274         
finally {
275             
try {
276                 
if(rs!=null)
277                     rs.close();
278
279                 
if(st!=null)
280                     st.close();
281
282                 
if(con!=null)
283                     con.close();
284             }
285             
catch(Exception ex) {}
286         }
287         
return model;
288     }
289 }


Gõ tìm kiếm nhanh...